package com.ky.manage.activity.emergency;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.ky.manage.R;
import com.ky.manage.activityLifecycle.ActivityManagerUtil;
import com.ky.manage.adapter.PicAddAdapter;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.constant.OverallData;
import com.ky.manage.model.req.EmergencyAccidentPublishReq;
import com.ky.manage.model.req.EmergencyPublishBaseReq;
import com.ky.manage.model.req.EmergencyTrainPublishReq;
import com.ky.manage.model.req.EmergencyXuanChuanPublishReq;
import com.ky.manage.model.response.UploadFileInfoResp;
import com.ky.manage.permission.AndPermissionUtils;
import com.ky.manage.ui.dialog.CommonDialog;
import com.ky.manage.utils.AddressRegionSelectUtils;
import com.ky.manage.utils.ImageLoader.GlideEngine;
import com.ky.manage.utils.ReClickHelper;
import com.ky.manage.utils.ScreenUtils;
import com.ky.manage.utils.TimeUtil;
import com.ky.manage.utils.ToastUtils;
import com.ky.manage.utils.ZyUtils;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.ky.manage.utils.http.BusinessHttpReqUtils;
import com.loopj.android.http.TextHttpResponseHandler;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.ky.manage.constant.OverallData.Emergency_Pei_Xun;
import static com.ky.manage.constant.OverallData.Emergency_Shi_Gu_Deal;
import static com.ky.manage.constant.OverallData.Emergency_Xuan_Chuan;
import static com.ky.manage.constant.OverallData.Emergency_Yan_Lian;

/**
 * 应急发布页
 */
public class EmergencyPublishActivity extends BaseActivity {
    public int mType;
    public String mPageTitle;
    public String mServiceApi;
    public static final int PIC_COVER_ADD = 1;
    public static final int PIC_LIST_ADD = 2;
    private List<LocalMedia> mPicSelectMediaList = new ArrayList<>();
    private List<LocalMedia> mCoverSelectMediaList = new ArrayList<>();
    private RecyclerView mPicRecyclerView, mCoverRecyclerView;
    private PicAddAdapter mPicAddAdapter, mCoverAddAdapter;
    private EditText titleEt, detailAddressEt, simpleDescEt, contentEt;
    private TextView timeSelectTv, regionSelectTv;
    /**
     * 1:封面
     * 2：图片列表
     */
    private int mSelectPciType = PIC_COVER_ADD;

    public static void toEmergencyPublishActivity(BaseActivity context, int type) {
        Intent intent = new Intent(context, EmergencyPublishActivity.class);
        intent.putExtra("type", type);
        context.toActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_emergency_publish;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });
        timeSelectTv = findView(R.id.time_select_tv, v -> {
            //时间选择
            AddressRegionSelectUtils.getInstance().showTimePicker(this, (date, v1) -> {
                timeSelectTv.setText(TimeUtil.getYmdHmsTimeFromDate(date));
            });
        });
        regionSelectTv = findView(R.id.region_select_tv, v -> {
            //区域选择
            AddressRegionSelectUtils.getInstance().showRegionSelect(this, new AddressRegionSelectUtils.OnRegionSelect() {
                @Override
                public void onSelect(String result) {
                    regionSelectTv.setText(result);
                }

                @Override
                public void onCancel() {

                }
            });
        });
        titleEt = findView(R.id.title_et);
        detailAddressEt = findView(R.id.detail_address_et);
        simpleDescEt = findView(R.id.desc_et);
        contentEt = findView(R.id.content_et);

        findView(R.id.publish_tv, v -> {
           //立即发布
            switch (mType) {
                case Emergency_Shi_Gu_Deal:
                    //事故处理
                    EmergencyAccidentPublishReq accidentPublishReq = new EmergencyAccidentPublishReq();
                    accidentPublishReq.title = titleEt.getText().toString();
                    accidentPublishReq.happenTime = timeSelectTv.getText().toString();
                    accidentPublishReq.faultAddress = regionSelectTv.getText().toString();
                    accidentPublishReq.detailAddress = detailAddressEt.getText().toString();
                    accidentPublishReq.faultDesc = simpleDescEt.getText().toString();
                    accidentPublishReq.faultAnalyse = contentEt.getText().toString();
                    if (!TextUtils.isEmpty(accidentPublishReq.title.trim()) &&
                            !TextUtils.isEmpty(accidentPublishReq.happenTime.trim()) &&
                            !TextUtils.isEmpty(accidentPublishReq.faultAddress.trim()) &&
                            !TextUtils.isEmpty(accidentPublishReq.detailAddress.trim()) &&
                            !TextUtils.isEmpty(accidentPublishReq.faultDesc.trim()) &&
                            !TextUtils.isEmpty(accidentPublishReq.faultAnalyse.trim())
                            && isCoverImgSelect() && isPicListSelect()) {
                        ZyUtils.getInstance().showLoadingDialog(this, "请求中，请稍候");
                        mNeedUploadImgList.clear();
                        mNeedUploadImgList.addAll(mPicSelectMediaList);
                        mNeedUploadImgList.remove(0);
                        mNeedUploadImgList.addAll(0, mCoverSelectMediaList);
                        if (ZyUtils.getInstance().isNetConnected()) {
                            uploadLoadImg(() -> {
                                //所有图片上传完成
                                ZyUtils.getInstance().insertLog("EmergencyPublish", "uploadLoadAllImg成功");
                                accidentPublishReq.coverUrl = mUploadedImgUrlList.get(0);
                                for (int i = 1; i < mUploadedImgUrlList.size(); i++) {
                                    accidentPublishReq.faultAnalyse += "\n" + "<img src=\"" + mUploadedImgUrlList.get(i) + "\">";
                                }
                                doRealPublishReq(accidentPublishReq);
                            });
                        }
                    } else {
                        ToastUtils.showRoundRectToast("请完善信息后再提交");
                    }
                    break;
                case Emergency_Xuan_Chuan:
                    //应急宣传
                    EmergencyXuanChuanPublishReq xuanChuanPublishReq = new EmergencyXuanChuanPublishReq();
                    xuanChuanPublishReq.publicizeTitle = titleEt.getText().toString();
                    xuanChuanPublishReq.publicizeTime = timeSelectTv.getText().toString();
                    xuanChuanPublishReq.addressName = regionSelectTv.getText().toString();
                    xuanChuanPublishReq.detailAddress = detailAddressEt.getText().toString();
                    xuanChuanPublishReq.simpleDesc = simpleDescEt.getText().toString();
                    xuanChuanPublishReq.publicizeDesc = contentEt.getText().toString();
                    if (!TextUtils.isEmpty(xuanChuanPublishReq.publicizeTitle.trim()) &&
                            !TextUtils.isEmpty(xuanChuanPublishReq.publicizeTime.trim()) &&
                            !TextUtils.isEmpty(xuanChuanPublishReq.addressName.trim()) &&
                            !TextUtils.isEmpty(xuanChuanPublishReq.detailAddress.trim()) &&
                            !TextUtils.isEmpty(xuanChuanPublishReq.simpleDesc.trim()) &&
                            !TextUtils.isEmpty(xuanChuanPublishReq.publicizeDesc.trim())
                            && isCoverImgSelect() && isPicListSelect()) {
                        ZyUtils.getInstance().showLoadingDialog(this, "请求中，请稍候");
                        mNeedUploadImgList.clear();
                        mNeedUploadImgList.addAll(mPicSelectMediaList);
                        mNeedUploadImgList.remove(0);
                        mNeedUploadImgList.addAll(0, mCoverSelectMediaList);
                        if (ZyUtils.getInstance().isNetConnected()) {
                            uploadLoadImg(() -> {
                                //所有图片上传完成
                                ZyUtils.getInstance().insertLog("EmergencyPublish", "uploadLoadAllImg成功");
                                xuanChuanPublishReq.coverUrl = mUploadedImgUrlList.get(0);
                                for (int i = 1; i < mUploadedImgUrlList.size(); i++) {
                                    xuanChuanPublishReq.publicizeDesc += "\n" + "<img src=\"" + mUploadedImgUrlList.get(i) + "\">";
                                }
                                doRealPublishReq(xuanChuanPublishReq);
                            });
                        }
                    } else {
                        ToastUtils.showRoundRectToast("请完善信息后再提交");
                    }
                    break;
                case Emergency_Pei_Xun:
                    //应急培训
                case Emergency_Yan_Lian:
                    //应急演练
                    EmergencyTrainPublishReq yanLianPublishReq = new EmergencyTrainPublishReq();
                    yanLianPublishReq.title = titleEt.getText().toString();
                    yanLianPublishReq.trainTime = timeSelectTv.getText().toString();
                    yanLianPublishReq.addressName = regionSelectTv.getText().toString();
                    yanLianPublishReq.detailAddress = detailAddressEt.getText().toString();
                    yanLianPublishReq.simpleDesc = simpleDescEt.getText().toString();
                    yanLianPublishReq.recordDesc = contentEt.getText().toString();
                    if (!TextUtils.isEmpty(yanLianPublishReq.title.trim()) &&
                            !TextUtils.isEmpty(yanLianPublishReq.trainTime.trim()) &&
                            !TextUtils.isEmpty(yanLianPublishReq.addressName.trim()) &&
                            !TextUtils.isEmpty(yanLianPublishReq.detailAddress.trim()) &&
                            !TextUtils.isEmpty(yanLianPublishReq.simpleDesc.trim()) &&
                            !TextUtils.isEmpty(yanLianPublishReq.recordDesc.trim())
                            && isCoverImgSelect() && isPicListSelect()) {
                        ZyUtils.getInstance().showLoadingDialog(this, "请求中，请稍候");
                        mNeedUploadImgList.clear();
                        mNeedUploadImgList.addAll(mPicSelectMediaList);
                        mNeedUploadImgList.remove(0);
                        mNeedUploadImgList.addAll(0, mCoverSelectMediaList);
                        if (ZyUtils.getInstance().isNetConnected()) {
                            uploadLoadImg(() -> {
                                //所有图片上传完成
                                ZyUtils.getInstance().insertLog("EmergencyPublish", "uploadLoadAllImg成功");
                                yanLianPublishReq.coverUrl = mUploadedImgUrlList.get(0);
                                for (int i = 1; i < mUploadedImgUrlList.size(); i++) {
                                    yanLianPublishReq.recordDesc += "\n" + "<img src=\"" + mUploadedImgUrlList.get(i) + "\">";
                                }
                                doRealPublishReq(yanLianPublishReq);
                            });
                        }
                    } else {
                        ToastUtils.showRoundRectToast("请完善信息后再提交");
                    }
                    break;
                default:
                    break;
            }
        });
    }

    /**
     * 是否选择封面
     * @return
     */
    private boolean isCoverImgSelect() {
        //content://media/external/file/32009
        String localCoverImgPath = getPathFromLocalMedia(mCoverSelectMediaList.get(0));
        ZyUtils.getInstance().insertLog("zhaoyan", "isCoverImgSelect localCoverImgPath = "
                + localCoverImgPath);
        return null != localCoverImgPath;
    }

    /**
     * 是否选择图片列表
     * @return
     */
    private boolean isPicListSelect() {
        return mPicSelectMediaList.size() > 1;
    }


    private List<String> mUploadedImgUrlList = new ArrayList<>();
    private List<LocalMedia> mNeedUploadImgList = new ArrayList<>();
    private int mCurUploadImgIndex = 0;
    /**
     * 图片上传
     * @param uploadAllImgCallback
     */
    private void uploadLoadImg(UploadAllImgCallback uploadAllImgCallback) {
        if (mCurUploadImgIndex >= mNeedUploadImgList.size()) {
            //图片上传完成，最终接口上传合并
            if (null != uploadAllImgCallback) {
                uploadAllImgCallback.onUploadSuccess();
            }
            return;
        }
        LocalMedia localMedia = mNeedUploadImgList.get(mCurUploadImgIndex);
        String localMediaPath = getRealPathFromUri(this, Uri.parse(localMedia.getPath()));
        ZyUtils.getInstance().insertLog("uploadLoadImg", "localMediaPath = " + localMediaPath);
        BusinessHttpReqUtils.getInstance().uploadFile(new File(localMediaPath), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                ZyUtils.getInstance().insertLog("uploadLoadImg", "onFailure statusCode = " + statusCode
                        + ", responseString = " + responseString);
                ToastUtils.showRoundRectToast("上传文件失败：" + responseString);
                ZyUtils.getInstance().hideLoadingDialog();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                ZyUtils.getInstance().insertLog("uploadLoadImg", "onSuccess statusCode = " + statusCode
                        + ", response = " + responseString);
                UploadFileInfoResp fileInfoResp = BusinessHttpReqUtils.getInstance().parseJsonObj(responseString, UploadFileInfoResp.class);
                if (null != fileInfoResp && fileInfoResp.code == 200) {
                    //上传成功
                    mUploadedImgUrlList.add(fileInfoResp.url.replace("\\", ""));
                    ++mCurUploadImgIndex;
                    uploadLoadImg(uploadAllImgCallback);
                } else {
                    ToastUtils.showRoundRectToast("上传文件失败");
                    ZyUtils.getInstance().hideLoadingDialog();
                }
            }
        });
    }

    /**
     * 最终接口上传
     * @param req
     */
    private void doRealPublishReq(EmergencyPublishBaseReq req) {
        if (null == req) {
            return;
        }
        JSONObject pubReq = null;
        try {
            String reqJson = new Gson().toJson(req);
            pubReq = new JSONObject(reqJson);
        } catch (JSONException e) {
            Log.d("doRealPublishReq", "e = " + e.toString());
        }
        AsyncHttpUtils.getInstance().postJson(this, BusinessHttpReqUtils.SERVICE_DOMAIN + mServiceApi, pubReq, new TextHttpResponseHandler(){
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                ZyUtils.getInstance().insertLog("doRealPublishReq", "onFailure statusCode = "
                        + statusCode + ", responseString = " + responseString);
                ZyUtils.getInstance().hideLoadingDialog();
                ToastUtils.showRoundRectToast("发布失败");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                ZyUtils.getInstance().insertLog("doRealPublishReq", "onSuccess statusCode = "
                        + statusCode + ", response = " + responseString);
                ZyUtils.getInstance().hideLoadingDialog();
                if (statusCode == 200) {
                    ToastUtils.showRoundRectToast("发布完成");
                    OverallData.hd.postDelayed((Runnable) () -> {
                        finish();
                    }, 300);
                } else {
                    ToastUtils.showRoundRectToast("发布失败! statusCode: " + statusCode);
                }
            }
        });
    }

    private void initPicListView() {
        mPicRecyclerView = findView(R.id.pic_list_view);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mPicRecyclerView.setLayoutManager(manager);
        mPicAddAdapter = new PicAddAdapter(R.layout.layout_pic_item_filter, mPicSelectMediaList);
        mPicRecyclerView.setAdapter(mPicAddAdapter);
        mPicAddAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mPicSelectMediaList.size() > 0) {
                LocalMedia media = mPicSelectMediaList.get(position);
                if (null == media.getPath()) {
                    //添加图片
                    mSelectPciType = PIC_LIST_ADD;
                    selectPic();
                    return;
                }
                //"image/jpeg"
                String mimeType = media.getMimeType();
                PictureSelector.create(EmergencyPublishActivity.this)
                        .externalPicturePreview(position -1, getPreviewMediaList(mPicSelectMediaList), 0);
            }
        });
    }

    private void selectPic() {
        if (ReClickHelper.clickEnable()) {
            AndPermissionUtils.getInstance().requestCameraPermission(this, () -> {
                showAddPicDialog();
            });
        }
    }

    private void initCoverListView() {
        mCoverRecyclerView = findView(R.id.cover_list_view);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mCoverRecyclerView.setLayoutManager(manager);
        mCoverAddAdapter = new PicAddAdapter(R.layout.layout_pic_item_filter, mCoverSelectMediaList);
        mCoverRecyclerView.setAdapter(mCoverAddAdapter);
        mCoverAddAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mCoverSelectMediaList.size() > 0) {
                LocalMedia media = mCoverSelectMediaList.get(position);
                if (null == media.getPath()) {
                    //添加图片
                    mSelectPciType = PIC_COVER_ADD;
                    selectPic();
                    return;
                }
                //"image/jpeg"
                String mimeType = media.getMimeType();
                PictureSelector.create(EmergencyPublishActivity.this)
                        .externalPicturePreview(position, mCoverSelectMediaList, 0);
            }
        });
    }

    /**
     * 获取图片路径
     * @param media
     * @return
     */
    public String getPathFromLocalMedia(LocalMedia media) {
        if (media == null || TextUtils.isEmpty(media.getPath())) {
            return null;
        }
        String path;
        if (media.isCut() && !media.isCompressed()) {
            // 裁剪过
            path = media.getCutPath();
        } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
            // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
            path = media.getCompressPath();
        } else {
            // 原图地址
            path = media.getPath();
        }
        return path;
    }

    public String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    private List<LocalMedia> getPreviewMediaList(List<LocalMedia> mediaList) {
        List<LocalMedia> previewMediaList = new ArrayList<>();
        for (int i = 1; i < mediaList.size(); i++) {
            previewMediaList.add(mediaList.get(i));
        }
        return previewMediaList;
    }

    @Override
    protected void initData() {
        mType = getIntent().getIntExtra("type", Emergency_Pei_Xun);
        mServiceApi = "";
        String timeLabel = "时间", regionLabel = "区域", contentLabel = "内容", picTitleLabel = "图片"
                , timeHint = "请选择时间", regionHint = "请选择区域", contentHint = "请输入内容";
        switch (mType) {
            case Emergency_Pei_Xun:
                mServiceApi = "/appApi/TrainingAdd";
                mPageTitle = "应急培训";
                timeLabel = "培训时间";
                timeHint = "请选择培训时间";
                regionLabel = "培训区域";
                regionHint = "请选择培训区域";
                picTitleLabel = "培训图片";
                contentLabel = "培训内容";
                contentHint = "请输入培训内容";
                break;
            case Emergency_Shi_Gu_Deal:
                mServiceApi = "/appApi/faultAdd";
                mPageTitle = "事故处理记录";
                timeLabel = "事故时间";
                timeHint = "请选择事故时间";
                regionLabel = "事故区域";
                regionHint = "请选择事故区域";
                picTitleLabel = "事故图片";
                contentLabel = "事故处理内容";
                contentHint = "请输入事故内容";
                break;
            case Emergency_Xuan_Chuan:
                mServiceApi = "/appApi/publicizeAdd";
                mPageTitle = "应急宣传";
                timeLabel = "宣传时间";
                regionLabel = "宣传区域";
                regionHint = "请选择宣传区域";
                timeHint = "请选择宣传时间";
                picTitleLabel = "宣传图片";
                contentLabel = "宣传内容";
                contentHint = "请输入宣传内容";
                break;
            case Emergency_Yan_Lian:
                mServiceApi = "/appApi/dirllAdd";
                mPageTitle = "应急演练";
                timeLabel = "培训演练";
                regionLabel = "演练区域";
                regionHint = "请选择演练区域";
                timeHint = "请选择演练时间";
                picTitleLabel = "演练图片";
                contentLabel = "演练内容";
                contentHint = "请输入演练内容";
                break;
            default:
                break;
        }
        findTextView(R.id.page_title_tv, mPageTitle);
        findTextView(R.id.time_tv, timeLabel);
        timeSelectTv.setHint(timeHint);
        findTextView(R.id.region_tv, regionLabel);
        regionSelectTv.setHint(regionHint);
        findTextView(R.id.pic_title_tv, picTitleLabel);
        findTextView(R.id.content_tv, contentLabel);
        contentEt.setHint(contentHint);

        mCoverSelectMediaList.add(new LocalMedia());
        mPicSelectMediaList.add(new LocalMedia());
        initCoverListView();
        initPicListView();
    }

    private CommonDialog mAddPicDialog;

    /**
     * 添加图片选择框
     */
    private void showAddPicDialog() {
        if (ActivityManagerUtil.getInstance().isActivityDestroy(this)) {
            return;
        }
        if (null == mAddPicDialog) {
            WeakReference<FragmentActivity> activityWeakReference = new WeakReference<>(this);
            mAddPicDialog = CommonDialog.create(activityWeakReference.get().getSupportFragmentManager())
                    .setLayoutRes(R.layout.dialog_add_pic)
                    .setViewListener(((v, baseDialog) -> {
                        if (null == activityWeakReference || null == activityWeakReference.get()
                                || activityWeakReference.get().isFinishing()) {
                            return;
                        }
                        TextView addPicFromCamera = v.findViewById(R.id.add_pic_camera);
                        TextView addPicFromAlbum = v.findViewById(R.id.add_pic_album);
                        TextView cancelTv = v.findViewById(R.id.cancel_tv);
                        addPicFromCamera.setOnClickListener(v1 -> {
                            //拍照获取图片
                            if (ReClickHelper.clickEnable()) {
                                //拍照
                                PictureSelector.create(EmergencyPublishActivity.this)
                                        .openCamera(PictureMimeType.ofImage())
                                        .forResult(PictureConfig.CHOOSE_REQUEST);
                                hideAddPicDialog();
                            }
                        });
                        addPicFromAlbum.setOnClickListener(v1 -> {
                            //从相册获取图片
                            if (ReClickHelper.clickEnable()) {
                                PictureSelector.create(EmergencyPublishActivity.this)
                                        .openGallery(PictureMimeType.ofImage())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                                        .maxSelectNum(mSelectPciType == PIC_COVER_ADD ? 1 : Integer.MAX_VALUE)// 最大图片选择数量
                                        .minSelectNum(1)// 最小选择数量
                                        .imageSpanCount(4)// 每行显示个数
                                        .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选PictureConfig.MULTIPLE : PictureConfig.SINGLE
                                        .imageEngine(GlideEngine.createGlideEngine())
                                        .isPreviewImage(true)// 是否可预览图片
                                        .isCamera(true)// 是否显示拍照按钮
                                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
//                                        .setOutputCameraPath(Environment.getExternalStorageDirectory().getPath()
//                                                + "/ky/tmpPath/")// 自定义拍照保存路径
                                        .isEnableCrop(false)// 是否裁剪
                                        .isCompress(false)// 是否压缩
                                        //.sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                                        .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                                        .withAspectRatio(1, 1)// 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                                        //.selectionMedia(selectList)// 是否传入已选图片
                                        //.previewEggs(false)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中)
                                        //.cropCompressQuality(90)// 裁剪压缩质量 默认100
                                        //.compressMaxKB()//压缩最大值kb compressGrade()为Luban.CUSTOM_GEAR有效
                                        //.compressWH() // 压缩宽高比 compressGrade()为Luban.CUSTOM_GEAR有效
                                        //.cropWH()// 裁剪宽高比，设置如果大于图片本身宽高则无效
                                        .rotateEnabled(false) // 裁剪是否可旋转图片
                                        //.scaleEnabled()// 裁剪是否可放大缩小图片
                                        //.recordVideoSecond()//录制视频秒数 默认60s
                                        .forResult(PictureConfig.CHOOSE_REQUEST);
                                hideAddPicDialog();
                            }
                        });
                        cancelTv.setOnClickListener(v1 -> {
                            //关闭弹框
                            hideAddPicDialog();
                        });
                    }))
                    .setDialogAnimationRes(R.style.Center_Zoom_Dialog)
                    .setGravity(Gravity.CENTER)
                    .setOnDismissListener(dialog -> {
                        //对话框关闭
                        hideAddPicDialog();
                    })
                    .setDialogWidth(ScreenUtils.dp2px(300))
                    .setDimAmount(0.2f)
                    .setCancelOutside(false)
                    .setKeyBackDismiss(false)
                    .setTag("AddPicDialog");
        }

        if (mAddPicDialog.isShowing()) {
            return;
        }
        mAddPicDialog.show();
    }

    private void hideAddPicDialog() {
        if (null != mAddPicDialog) {
            mAddPicDialog.dismissAllowingStateLoss();
            mAddPicDialog = null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED) {
            return;
        }
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                // 图片选择结果回调
                images = PictureSelector.obtainMultipleResult(data);
                if (null == images || images.size() == 0) {
                    return;
                }
                //拍照：
                // path:/storage/emulated/0/PictureSelector/CameraImage/PictureSelector_20200202_114244.JPEG
                //cutPicPath: null, compressPicPath: null
                //相册
                // path:/storage/emulated/0/Pictures/Screenshots/Screenshot_20200202_100645_lxkj.train.com.jpg
                //cutPicPath: null, compressPicPath: null
                if (mSelectPciType == PIC_COVER_ADD) {
                    mCoverSelectMediaList.clear();
                    mCoverSelectMediaList.addAll(images);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    mCoverAddAdapter.setList(mCoverSelectMediaList);
                    mCoverAddAdapter.notifyDataSetChanged();
                } else {
                    mPicSelectMediaList.addAll(images);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    mPicAddAdapter.setList(mPicSelectMediaList);
                    mPicAddAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    public interface UploadAllImgCallback {
        void onUploadSuccess();
    }

}